/**
 * Copyright 2016-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.inmorn.extspring.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import com.inmorn.extspring.metadata.Page;
import com.inmorn.extspring.metadata.PageCache;
import com.inmorn.extspring.metadata.QueryPage;

public class JqGridBaseController {

	/**
	 * 
	 */
	//已选择的ID数组
	private String[] selectIds;
	// 是否是查询，值是true或者false
	private boolean _search; 
	// 每页显示的记录条数
	private int rows; 
	// 查询第几页的数据
	private int page;
	// 步长
	private int rowNum;
	
	private String nd;
	// 查询排序的条件
	private String sidx;
	// 查询排序的方式，可能的值是ASC和DESC
	private String sord;
	@SuppressWarnings("rawtypes")
	protected Page result;
	protected Map<String, String> searchFields = new HashMap<String, String>(0);
	protected Map<String, String[]> searchArrays = new HashMap<String, String[]>(
			0);
	
	//分页参数对象
	protected QueryPage queryPage;
	
	//页面缓存
	protected PageCache pageCache = new PageCache();

	@SuppressWarnings({ "unchecked", "unused" })
	public void populateJqGridData(HttpServletRequest request) {
		// 是否导航到缓存的页面
		String isPageCache = request.getParameter("pageCache");
		String loadPageCache = request.getParameter("loadPageCache");
		
		Map<String, String[]> parameters = request
				.getParameterMap();
		Set<String> keys = parameters.keySet();
		for (String key : keys) {
			String[] values = parameters.get(key);

			if ("sidx".equals(key)) {
				if (null != values && values.length > 0)
					sidx = values[0];
			} else if ("sord".equals(key)) {
				if (null != values && values.length > 0)
					sord = values[0];
			} else if ("_search".equals(key)) {
				if (null != values && values.length > 0)
					_search = Boolean.parseBoolean(values[0]);
			} else if ("rows".equals(key)) {
				if (null != values && values.length > 0)
					rows = Integer.parseInt(values[0]);
			} else if ("page".equals(key)) {
				if (null != values && values.length > 0)
					try{ // 处理因为输入的翻页为非法字符时，报错的问题
						page = Integer.parseInt(values[0].trim()) - 1; // 从0开始
					}catch(NumberFormatException e){
					}
			} else if ("pageCache".equals(key) || "loadPageCache".equals(key)) {
				
//			} else if ("pageCache".equals(key)) {
//				if (null != values && values.length > 0)
//					isPageCache = values[0];
			} else if ("nd".equals(key)) {
				if (null != values && values.length > 0)
					nd = values[0];
			} else if ("rowNum".equals(key)) {
				if (null != values && values.length > 0) {
					rowNum = Integer.parseInt(values[0]);
			}
			}else if ("id".equals(key)) {
				if (null != values && values.length > 0) {
					selectIds = values[0].split(",");
				}
			} else if (key.endsWith("Array")) {
				// 数组参数
				searchArrays.put(key, values);
			} else {
				// 其他参数
				// System.out.println("####################key:" + key +
				// ",values:" + values);
				if (null != values && values.length > 0)
					searchFields.put(key, values[0]);
				// System.out.println("####################key:" + key +
				// ",value:" + values[0]);
			}

		}

	}
	
	

	public Map<String, String[]> getSearchArrays() {
		return searchArrays;
	}

	public void setSearchArrays(Map<String, String[]> searchArrays) {
		this.searchArrays = searchArrays;
	}

	public boolean is_Search() {
		return _search;
	}

	public void set_Search(boolean _search) {
		this._search = _search;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public String getNd() {
		return nd;
	}

	public void setNd(String nd) {
		this.nd = nd;
	}

	public String getSidx() {
		return sidx;
	}

	public void setSidx(String sidx) {
		this.sidx = sidx;
	}

	public String getSord() {
		return sord;
	}

	public void setSord(String sord) {
		this.sord = sord;
	}

	@SuppressWarnings("rawtypes")
	public <T>Page getResult() {
		return this.result;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public  <T> void setResult(Page<T> result) {
		if(result == null){
			result = new Page<T>(new ArrayList(), 0, 0, 0);
		}
		if (result.getTotal() <= result.getPage()) {
			result.setPage(result.getTotal());
		} else {
			result.setPage(result.getPage()+1); // jqgrid 当前页从1开始
		}
		this.result = result;
	}

	public int getRowNum() {
		return rowNum;
	}

	public void setRowNum(int rowNum) {
		this.rowNum = rowNum;
	}

	public Map<String, String> getSearchFields() {
		return searchFields;
	}

	public void setSearchFields(Map<String, String> searchFields) {
		this.searchFields = searchFields;
	}



	public String[] getSelectIds() {
		return selectIds;
	}



	public void setSelectIds(String[] selectIds) {
		this.selectIds = selectIds;
	}

	protected void setPageProperties(){
		/*
		pageCache.putPageProperties("rows", rows);
		pageCache.putPageProperties("page", page);
		pageCache.putPageProperties("rowNum", rowNum);
		pageCache.putPageProperties("nd", nd);
		pageCache.putPageProperties("sidx", sidx);
		pageCache.putPageProperties("sord", sord);
		*/
	}



	public void setQueryPage(QueryPage queryPage) {
		this.queryPage = queryPage;
	}

	public QueryPage getQueryPage() {
		return queryPage;
	}
	
}
